package com.ellfors.module_first.main

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import com.ellfors.common.base.adapter.BaseDiffAdapter
import com.ellfors.module_first.R
import com.ellfors.module_first.databinding.ItemMainBinding

private val ITEM_CALLBACK = object : DiffUtil.ItemCallback<Pair<String, () -> Unit>>() {
    override fun areItemsTheSame(
        oldItem: Pair<String, () -> Unit>, newItem: Pair<String, () -> Unit>
    ): Boolean {
        return oldItem.first == newItem.first
    }

    override fun areContentsTheSame(
        oldItem: Pair<String, () -> Unit>, newItem: Pair<String, () -> Unit>
    ): Boolean {
        return oldItem.first == newItem.first
    }
}

class MainAdapter :
    BaseDiffAdapter<Pair<String, () -> Unit>, ItemMainBinding>(ITEM_CALLBACK) {

    init {
        addOnItemChildClickListener(R.id.btn_navigation) { _, _, position ->
            items[position].second.invoke()
        }
    }

    override val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> ItemMainBinding
        get() = ItemMainBinding::inflate

    override fun convert(
        holder: DiffViewHolder<ItemMainBinding>, position: Int, item: Pair<String, () -> Unit>?
    ) {
        holder.binding.btnNavigation.text = item?.first ?: ""
    }
}